<?php

namespace JanVoracek\Forms\Rendering;
/**
 * Scaffolding Renderer.
 *
 * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
 * Copyright (c) 2011 Jan Voráček
 *
 * This source file is subject to the New BSD License.
 * For more information please see http://addons.nettephp.com/scaffoldingrenderer
 *
 * @package    Nette Addons
 */

use \Nette\Utils\Html, 
		\Nette\Forms\Rendering\DefaultFormRenderer, 
		\Nette\Forms\IControl, 
		\Nette\Forms\Controls\Checkbox, 
		\Nette\Forms\Controls\Button;
		

/**
 * Builds a latte template.
 *
 * @author     David Grudl, Jan Voráček
 * @copyright  Copyright (c) 2008, 2009 David Grudl, 2011 Jan Voráček
 * @package    Nette Extras
 */
class LatteScaffoldingRenderer extends DefaultFormRenderer
{
	/**
	 * Renders form begin.
	 * @return string
	 */
	public function renderBegin()
	{
		return "{form ".$this->form->name."}";
	}



	/**
	 * Renders form end.
	 * @return string
	 */
	public function renderEnd()
	{
		return "{/form}";
	}



	/**
	 * Renders validation errors (per form or per control).
	 * @param  IControl
	 * @return void
	 */
	public function renderErrors(IControl $control = NULL)
	{
		return "{control \$form errors}"; // no macro for form errors
	}



	/**
	 * Renders single visual row of multiple controls.
	 * @param  array of IControl
	 * @return string
	 */
	public function renderPairMulti(array $controls)
	{
		$s = array();
		foreach ($controls as $control) {
			if (!($control instanceof IControl)) {
				throw new InvalidArgumentException("Argument must be array of IControl instances.");
			}
			$control->setOption('rendered', TRUE);
			$name = $control->lookupPath('Nette\Forms\Form');
			$s[] = "{input $name}";
		}
		$pair = $this->getWrapper('pair container');
		$pair->add($this->getWrapper('label container')->setHtml('&nbsp;'));
		$pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s)));
		return $pair->render(0);
	}



	/**
	 * Renders 'label' part of visual row of controls.
	 * @param  IControl
	 * @return string
	 */
	public function renderLabel(IControl $control)
	{
		$head = $this->getWrapper('label container');

		if ($control instanceof Checkbox || $control instanceof Button) {
			return $head->setHtml('&nbsp;');

		} else {
			$name = $control->lookupPath('Nette\Forms\Form');
			return $head->setHtml("{label $name}" . $this->getValue('label suffix'));
		}
	}



	/**
	 * Renders 'control' part of visual row of controls.
	 * @param  IControl
	 * @return string
	 */
	public function renderControl(IControl $control)
	{
		$body = $this->getWrapper('control container');
		if ($this->counter % 2) $body->class($this->getValue('control .odd'), TRUE);

		$description = $control->getOption('description');
		if ($description instanceof Html) {
			$description = ' ' . $control->getOption('description');

		} elseif (is_string($description)) {
			$description = ' ' . $this->getWrapper('control description')->setText($description);

		} else {
			$description = '';
		}

		if ($this->getValue('control errors')) {
			$description .= $this->renderErrors($control);
		}

		$control->setOption('rendered', TRUE);
		$name = $control->lookupPath('Nette\Forms\Form');
		if ($control instanceof Checkbox || $control instanceof Button) {
			return $body->setHtml("{input $name}{label \$form['$name']}" . $description);

		} else {
			return $body->setHtml("{input $name}" . $description);
		}
	}

}
